﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

namespace PHTShop
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        List<Items> cart = new List<Items>();

        public List<Items> Cart
        {
            get { return cart; }
            set { cart = value; }
        }

        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Thanks";
            if (!IsPostBack)
            {
                List<Items> cart = (List<Items>)Session["Cart"];
                this.Cart = cart;

                switch (Session["languages"].ToString().ToLower())
                {
                    case "france":
                        Page.Title = "Merci";
                        break;
                    case "germany":
                        Page.Title = "Dank";
                        break;
                    default:
                        Page.Title = "Thanks";
                        break;
                }
                this.submitOrder();
                this.sendMail();
                Session.Remove("Cart");
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        private decimal total()
        {
            decimal s = 0;
            foreach (Items it in cart)
                s += it.pro.price.Value * it.qua;
            return s;
        }

        private void sendMail()
        {
            Account ac = Session["Account"] as Account;
            Setting email = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Email"));
            Setting password = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Password"));
            Setting smtp = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Smtp"));
            Setting port = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("port"));
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(email.value);
                msg.To.Add(ac.email);
                msg.Subject = "Thanks for order ! " + ac.fullname;

                string str = string.Empty;

                for (int i = 0; i < this.Cart.Count; i++)
                {
                    str += "ProducId : " + Cart[i].pro.id.ToString();
                    str += "\nProduc Name : " + Cart[i].prolg.name.ToString();
                    str += "\nPrice : " + Cart[i].pro.price.ToString();
                    str += "\nQuantity : " + Cart[i].qua.ToString();
                    str += "\n\n";
                }

                msg.Body = str + "Total : " + total().ToString();

                SmtpClient sc = new SmtpClient(smtp.value);
                sc.Port =Convert.ToInt32(port.value);
                sc.Credentials = new NetworkCredential(email.value, password.value);
                sc.EnableSsl = true;
                sc.Send(msg);
            }
            catch
            {

            }
        }

        private void submitOrder()
        {
            if (cart != null)
            {
                Account ac = Session["Account"] as Account;

                Order order = new Order();
                order.paymentId = Convert.ToInt32(Session["paymentId"]);
                order.datecreation = DateTime.Now;
                order.accountId = ac.id;
                order.name = "Order " + (order.id + 1);
                order.status = false;

                this.dc.Orders.InsertOnSubmit(order);
                this.dc.SubmitChanges();


                int id = order.id;
                foreach (Items it in cart)
                {
                    OrderDetail orderdt = new OrderDetail();
                    orderdt.orderId = id;
                    orderdt.productsId = it.pro.id;
                    orderdt.quantity = it.qua;
                    orderdt.price = it.pro.price;

                    this.dc.OrderDetails.InsertOnSubmit(orderdt);
                    this.dc.SubmitChanges();
                }
             }
        }
    }
}